<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/7/5
  Time: 15:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>添加页面</title>
    <script src="static/js/jquery-2.1.0.js"></script>
</head>
<body>
        <form action="${pageContext.request.contextPath}/user?method=add" method="post" onsubmit="return checkNull()">
                用户名：<input id="aaa" type="text" name="username" onblur="deleteSpan(this)"><span style="color: red" id="aaaa"></span><br>
                密码：<input id="bbb" type="text" name="password" onblur="deleteSpan(this)"><span style="color: red" id="bbbb"></span><br>
            <input type="submit" value="添加" id="submit"    >
        </form>
</body>
    <script>
        function checkNull() {
            let personname=document.getElementById("aaa").value;
            let personage=document.getElementById("bbb").value;
            if (personname==null ||personname==''){
                document.getElementById("aaaa").innerHTML="用户名不能为空";
                /*alert('人员姓名不能为空');*/
                document.getElementById("aaa").focus();
                return false;
            }
            if (personage==null ||personage==''){
                document.getElementById("bbbb").innerHTML="密码不能为空";
                return false;
            }
           return true;
        }

        function deleteSpan(obj) {
                //校验用户名是否被占用
                let  username=$("#aaa").val();
                //发送ajax请求
                $.ajax({
                    url:"user?method=findUserByUsername",
                    data:{username:username},//传递给后台的参数
                    dataType:"json",//相应的数据类型：服务器给我的
                    type:"get",
                    success:function (data) {
                        if(data.check){
                            $("#aaaa").html("用户名可以使用");
                            $("#submit").removeAttr("disabled")
                        }else
                        {
                            $("#aaaa").html("用户名不可以使用，请跟换");
                            $("#submit").attr("disabled","disabled")
                        }
                    }


                })
               if(obj.value!=null&&obj.value!=''){
                   let elementsByTagName = document.getElementsByTagName("span");
                   for (let i = 0; i < elementsByTagName.length; i++) {
                       elementsByTagName[i].innerHTML='';
                   }
               }
        }
    </script>
</html>
